/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dnbW.dbW;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

/**
 *
 * @author Truong Ba Quan
 */
public class DatabaseList extends AbstractListModel implements ComboBoxModel{

    private boolean hasDefault = false;
    private List<DatabaseInfo> databases;
    private DatabaseInfo curDatabase = null;
        
    // <editor-fold desc="Property Changes Support">
    private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public void addCurrentDatabasePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener("curDatabase", listener);
    }

    public void removeCurrentDatabasePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener("curDatabase", listener);
    }
    // </editor-fold>

    public DatabaseList() {
        this.databases = new Vector<DatabaseInfo>();
        this.databases.add(DatabaseInfo.DEFAULT_DATABASE);
        this.hasDefault = true;
    }

    @Override
    public int getSize() {
        return this.databases.size();
    }

    @Override
    public Object getElementAt(int index) {
        return this.databases.get(index);
    }

    @Override
    public void setSelectedItem(Object anItem) {
        DatabaseInfo oldDatabase = this.curDatabase;
        this.curDatabase = (DatabaseInfo)anItem;
        this.fireContentsChanged(this, -1, -1);
        changeSupport.firePropertyChange("curDatabase", oldDatabase, this.curDatabase);
    }

    @Override
    public Object getSelectedItem() {
        return this.curDatabase;
    }
    
    public void add(int index, DatabaseInfo db) {
        this.databases.add(index, db);
        //this.fireIntervalAdded(this, index, index);
    }

    public void addElement(DatabaseInfo db) {
        this.databases.add(db);
    }

    public DatabaseInfo remove(int index) {
        DatabaseInfo removed = this.databases.remove(index);
        //this.fireIntervalRemoved(this, index, index);
        return removed;
    }

    public boolean removeElement(DatabaseInfo db) {
        int index = -1;
        for (int i = 0; i < this.databases.size(); i++) {
            if (db.equals(this.databases.get(i))) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            this.databases.remove(index);
            //this.fireIntervalRemoved(this, index, index);
            return true;
        }
        else {
            return false;
        }
    }

    public Collection<DatabaseInfo> getDatabases() {
        if (hasDefault) {
            return this.databases.subList(1, this.databases.size()); // omit the first default database
        }
        else {
            return this.databases;
        }
    }
}
